package com.bigkoo.pickerview.view;

import com.bigkoo.pickerview.ResourceTable;
import com.bigkoo.pickerview.Utils.ResUtil;
import com.bigkoo.pickerview.configure.PickerOptions;
import com.bigkoo.pickerview.listener.OnDismissListener;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.Window;
import ohos.app.Context;
import ohos.multimodalinput.event.KeyEvent;
import ohos.multimodalinput.event.TouchEvent;

/**
 * Created by Sai on 15/11/22.
 * 精仿iOSPickerViewController控件
 */
public class BasePickerView {

    private final int DIALOG_HEIGHT = 730;
    protected ComponentContainer contentContainer;
    protected PickerOptions mPickerOptions;
    protected Component clickView;//是通过哪个View弹出的
    private Context context;
    private ComponentContainer rootView;//附加View 的 根View
    private ComponentContainer dialogView;//附加Dialog 的 根View
    private OnDismissListener onDismissListener;
    private boolean dismissing;
    private boolean isShowing;
    private CommonDialog mDialog;
    /**
     * Called when the user touch on black overlay, in order to dismiss the dialog.
     */
    private final Component.TouchEventListener onCancelableTouchListener = new Component.TouchEventListener() {
        @Override
        public boolean onTouchEvent(Component component, TouchEvent event) {
            if (event.getAction() == TouchEvent.NONE) {
                dismiss();
            }
            return false;
        }
    };
    private Component.KeyEventListener onKeyBackListener = new Component.KeyEventListener() {
        @Override
        public boolean onKeyEvent(Component component, KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.KEY_BACK && event.getKeyCode() == KeyEvent.KEY_PRESSED && isShowing()) {
                dismiss();
                return true;
            }
            return false;
        }
    };

    public BasePickerView(Context context) {
        this.context = context;
    }

    protected void initViews() {

        StackLayout.LayoutConfig params = new StackLayout.LayoutConfig(StackLayout.LayoutConfig.MATCH_PARENT, StackLayout.LayoutConfig.MATCH_CONTENT, LayoutAlignment.BOTTOM);
        LayoutScatter layoutInflater = LayoutScatter.getInstance(context);

        if (isDialog()) {
            dialogView = (ComponentContainer) layoutInflater.parse(ResourceTable.Layout_layout_basepickerview, null, false);
            dialogView.setBackground(ResUtil.buildDrawableByColor(Color.TRANSPARENT.getValue()));
            contentContainer = (ComponentContainer) dialogView.findComponentById(ResourceTable.Id_content_container);

            //params.setMarginLeft(30);
            //params.setMarginRight(30);
            params.height = DIALOG_HEIGHT;
            contentContainer.setLayoutConfig(params);
            createDialog();
            dialogView.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    dismiss();
                }
            });
        } else {
            if (mPickerOptions.decorView == null) {

                // DecorView decorView=new DecorView(context);
                // StackLayout stackLayout=new StackLayout(context);
                // mPickerOptions.decorView=stackLayout;

            }
            //将控件添加到decorView中
            rootView = (ComponentContainer) layoutInflater.parse(ResourceTable.Layout_layout_basepickerview, mPickerOptions.decorView, false);
            rootView.setLayoutConfig(new StackLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT));
            if (mPickerOptions.outSideColor != -1) {
                rootView.setBackground(ResUtil.buildDrawableByColor(mPickerOptions.outSideColor));
            }
            //这个是真正要加载时间选取器的父布局

            contentContainer = (ComponentContainer) rootView.findComponentById(ResourceTable.Id_content_container);
            contentContainer.setLayoutConfig(params);
        }
        setKeyBackCancelable(true);
    }

    protected void initEvents() {
    }

    /**
     * This function shows a dialog with given component
     *
     * @param v (是通过哪个View弹出的)
     */
    public void show(Component v) {
        this.clickView = v;
        show();
    }

    /**
     * 添加View到根视图
     */
    public void show() {
        if (isDialog()) {
            showDialog();
        } else {
            if (isShowing()) {
                return;
            }
            isShowing = true;
            onAttached(rootView);
            rootView.requestFocus();
        }
    }

    /**
     * show的时候调用
     *
     * @param view 这个View
     */
    private void onAttached(Component view) {
        mPickerOptions.decorView.addComponent(view);
    }

    /**
     * 检测该View是不是已经添加到根视图
     *
     * @return 如果视图已经存在该View返回true
     */
    public boolean isShowing() {
        if (isDialog()) {
            return false;
        } else {
            return rootView.getComponentParent() != null || isShowing;
        }
    }

    public void dismiss() {
        if (isDialog()) {
            dismissDialog();
        } else {
            if (dismissing) {
                return;
            }
            dismissImmediately();
            dismissing = true;
        }
    }

    public void dismissImmediately() {

        mPickerOptions.decorView.removeComponent(rootView);
        isShowing = false;
        dismissing = false;
        if (onDismissListener != null) {
            onDismissListener.onDismiss(BasePickerView.this);
        }
    }

    public BasePickerView setOnDismissListener(OnDismissListener onDismissListener) {
        this.onDismissListener = onDismissListener;
        return this;
    }

    public void setKeyBackCancelable(boolean isCancelable) {

        ComponentContainer View;
        if (isDialog()) {
            View = dialogView;
        } else {
            View = rootView;
        }
        View.setFocusable(Component.FOCUS_ENABLE);
        View.setTouchFocusable(isCancelable);
        if (isCancelable) {
            View.setKeyEventListener(onKeyBackListener);
        } else {
            View.setKeyEventListener(null);
        }
    }

    protected BasePickerView setOutSideCancelable(boolean isCancelable) {

        if (rootView != null) {
            Component view = rootView.findComponentById(ResourceTable.Id_content_container);

            if (isCancelable) {
                view.setTouchEventListener(onCancelableTouchListener);
            } else {
                view.setTouchEventListener(null);
            }
        }
        return this;
    }

    /**
     * 设置对话框模式是否可以点击外部取消
     */
    public void setDialogOutSideCancelable() {
        if (mDialog != null) {
            mDialog.siteRemovable(mPickerOptions.cancelable);
        }
    }

    public Component findViewById(int id) {
        return contentContainer.findComponentById(id);
    }

    public void createDialog() {
        if (dialogView != null) {

            mDialog = new CommonDialog(context);
            mDialog.siteRemovable(mPickerOptions.cancelable);
            mDialog.setContentCustomComponent(dialogView);
            mDialog.setTransparent(true);
            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {

                dialogWindow.setLayoutAlignment(LayoutAlignment.CENTER);
                dialogWindow.setTransparent(true);
            }
            mDialog.setDestroyedListener(new CommonDialog.DestroyedListener() {
                @Override
                public void onDestroy() {
                    if (onDismissListener != null) {
                        onDismissListener.onDismiss(BasePickerView.this);
                    }
                }
            });
        }
    }

    private void showDialog() {
        if (mDialog != null) {
            mDialog.show();
        }
    }

    private void dismissDialog() {
        if (mDialog != null) {
            mDialog.hide();
        }
    }

    public ComponentContainer getDialogContainerLayout() {
        return contentContainer;
    }

    public CommonDialog getDialog() {
        return mDialog;
    }

    public boolean isDialog() {
        return false;
    }

}


